{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<style>
    .layui-laydate-header {
        display: none;
    }

    .layui-laydate-content > table > thead {
        display: none;
    }

    .laydate-day-prev {
        display: none;
    }

    .laydate-day-next {
        display: none;
    }

    .pl_btn {
        color: #4c9cfb !important;
        cursor: pointer;
    }
</style>
<form class="layui-form p-4">
    <h3 class="pb-3">功能菜单/节点</h3>
    <table class="layui-table layui-table-form">
        <tr>
            <td class="layui-td-gray">销售单位</td>
            <td>
                <select name="saleUnitId" lay-search >
                    <option value="">请选择销售单位</option>
                    {volist name=":get_department()" id="vo"}
                    <option value="{$vo.id}" {if isset($detail.saleUnitId) && $detail.saleUnitId==$vo.id} selected{/if}>{$vo.title}</option>
                    {/volist}
                </select>
            </td>
        </tr>
        <tr>
            <td class="layui-td-gray-2">单号<font>*</font>
            </td>
            <td>
                <input type="text" name="orderNum" value="{$detail.orderNum|default=''}" lay-verify="required" readonly
                       autocomplete="off" placeholder="请输入名称"
                       class="layui-input disabled">
            </td>
            <td class="layui-td-gray">供应商<font>*</font>
            </td>
            <td>
                <select name="punterId" id="punterId" lay-search lay-filter="punterId">
                    <option value="">选择供应商</option>
                    {volist name='punterList' id='pl'}
                    <option value="{$pl.id}" {if isset($detail.punterId)&&$detail.punterId==$pl.id} selected {/if}>{$pl.name}</option>
                    {/volist}
                </select>
            </td>
        </tr>
        <tr>
            <td class="layui-td-gray-2">出库人<font>*</font>
            </td>
            <td>
              {$detail.checkName|default=''}
            </td>
            <td class="layui-td-gray-2">出库时间<font>*</font>
            </td>
            <td>
                {$detail.checkTime|default=''}
            </td>
            <td class="layui-td-gray-2">状态<font>*</font>
            </td>
            <td>
                {if isset($detail.status)}
                {$detail.status==1?"草稿":"已确认"}
                {/if}
            </td>
        </tr>
        <tr>
            <td colspan="6"><h4>明细</h4></td>
        </tr>
        <tr>

            <td colspan="6">
                {if !isset($detail.status)||$detail.status==1}
                <a href="javascript:" data-url="{:url('addMx')}" data-width="1000"
                   class="layui-btn layui-btn-primary " onclick="getBjd()">关联采购合同</a>
                {/if}
                <table class="layui-table tableMany">
                    <thead>
                    <tr>
                        <td>序号</td>
                        <td>物料编码</td>
                        <td>品牌</td>
                        <td>品名</td>
                        <td>型号</td>
                        <td>订货号</td>
                        <td>计量单位</td>
                        <td>仓库</td>
                        <td>出库数量</td>
                        <td>库存数量</td>
                        <td>关联合同</td>
                        <td>货商合同</td>
                        <td>操作</td>
                    </tr>
                    </thead>
                    <tbody id="mxTbdy">
                    {volist name='mxList' id='ml' key='k'}
                    <tr>
                        <td>
                            <span class="xuhao">{$k}</span>
                            <input type="hidden" name="mx[mxid][]" value="{$ml.id}">
                            <input type="hidden" name="mx[mdId][]" class="mx_mdId" value="{$ml.mdId}">
                            <input type="hidden" name="mx[thisId][]" class="mx_thisId" value="{$ml.thisId}">
                            <input type="hidden" name="mx[sodPrice][]" class="" value="{$ml.sodPrice}">
                            <input type="hidden" class="mx_saleContractDetailId" name="mx[saleContractDetailId][]" value="{$ml.saleContractDetailId}">
                        </td>
                        <td>{$ml.bar_code}</td>
                        <td>{$ml.brand}</td>
                        <td>{$ml.mdName}</td>
                        <td>{$ml.specs}</td>
                        <td>{$ml.buyCode}</td>
                        <td>{$ml.unit}</td>
                        <td><select name="mx[wId][]" id="" lay-filter="mxWid">
                            {volist name="warehouse" id="wl"}
                            <option value="{$wl.id}" {if $ml.wId==$wl.id} selected {/if}>{$wl.name}</option>
                            {/volist}
                        </select></td>
                        <td><input type="text" name="mx[num][]" value="{$ml.num}" class="layui-input mx_num" onblur="getMoneys(this)"
                                   lay-verify="required" oninput="digitalFiltering(this,2)"></td>
                        <td><input type="text" name="mx[kcNum][]" value="{$ml.kcNum}" class="layui-input disabled kcNum" readonly
                                   lay-verify="required" ></td>
                        <td><input type="text" name="mx[saleContractOrder][]" class="layui-input" value="{$ml.saleContractOrder}" readonly></td>
                        <td><input type="text" name="mx[hsContract][]" class="layui-input" value="{$ml.hsContract}" ></td>
                        <td>
                            {if !isset($detail.status)||$detail.status==1}
                            <a href="javascript:" class="layui-btn layui-btn-xs layui-btn-danger"
                               onclick="delMx('{$ml.id}',this)">删除</a>
                            {/if}

                        </td>
                    </tr>
                    {/volist}
                    </tbody>
                </table>

            </td>
        </tr>
    </table>
    <div class="py-3">
        <input type="hidden" name="id" value="{$id}">
        {if !isset($detail.status)||$detail.status==1}
        <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform">保存</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        {/if}
        {if isset($detail.status)&&$detail.status==1}
        <a href="javascript:" class="layui-btn layui-btn-normal" onclick="onSure()">确认</a>
        {/if}

        {if isset($detail.status)&&$detail.status==2}
        <a href="javascript:" class="layui-btn layui-btn-warm" onclick="onCancel()">撤销</a>
        {/if}
    </div>
</form>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
    const moduleInit = ['tool'];
    function gouguInit() {
        var form = layui.form, tool = layui.tool, level = 0, laydate = layui.laydate;
        //监听提交
        form.on('submit(webform)', function (data) {
            let callback = function (e) {
                layer.msg(e.msg);
                if (e.code == 0) {
                    setTimeout(function () {
                        parent.parent.location.reload();
                    }, 1000);
                }
            }
            tool.post("{:url('add')}", data.field, callback);
            return false;
        });
        // form.on('select(mxWid)', function (data) {
        //     let value=data.value;
        //     let that=data.othis;
        //     let trObj = $(that).parent().parent();
        //     let mdId=trObj.find('.mx_mdId').val();
        //     let saleContractDetailId=trObj.find('.mx_saleContractDetailId').val();
        //     let param={
        //         mdId:mdId,
        //         saleContractDetailId:saleContractDetailId,
        //         wId:value
        //     };
        //     httpRequest("{:url('api/geKcMxInfo')}",param,function (res) {
        //         if (res.code==0){
        //             let data=res.data;
        //             let outNum=data.outNum;
        //             if (!outNum){
        //                 outNum=0;
        //             }
        //             let kcNum=parseFloat(data.num-outNum)
        //             trObj.find('.kcNum').val(kcNum);
        //             trObj.find('.mx_thisId').val(data.id);
        //         }
        //     },false)
        // });

    }

    let mxIndex = '';

    function saveAddMx(ojb) {
        var lastListItem = $('.tableMany tr:last');
        let aa = $(lastListItem).find(".xuhao")
        layer.close(mxIndex)
        console.log('aaa', ojb)
        let len = $(aa).text()
        if (!parseInt(len)) {
            len = 0;
        }
        let index = parseInt(len) + 1;
        let str = '';
        for (var i = 0; i < ojb.length; i++) {
            let item = ojb[i];
            str += '     <tr>\n' +
                '                        <td>\n' +
                '                            <span class="xuhao">'+index+'</span>\n' +
                '                            <input type="hidden" name="mx[mxid][]" value="">\n' +
                '                            <input type="hidden" class="mx_thisId" name="mx[thisId][]" value="'+item.id+'">\n' +
                '                            <input type="hidden" class="mx_mdId" name="mx[mdId][]" value="'+item.mdId+'">\n' +
                '                            <input type="hidden" class="" name="mx[sodPrice][]" value="'+item.sodPrice+'">\n' +
                '                            <input type="hidden" class="mx_saleContractDetailId" name="mx[saleContractDetailId][]" value="'+item.saleContractDetailId+'">\n' +
                '                        </td>\n' +
                '                        <td>'+item.bar_code+'</td>\n' +
                '                        <td>'+item.brand+'</td>\n' +
                '                        <td>'+item.mdName+'</td>\n' +
                '                        <td>'+item.specs+'</td>\n' +
                '                        <td>'+item.buyCode+'</td>\n' +
                '                        <td>'+item.unit+'</td>\n' +
                '                        <td><select name="mx[wId][]" id="" lay-filter="mxWid">\n' +
                '                            {volist name="warehouse" id="wl"}\n' +
                '                            <option value="{$wl.id}">{$wl.name}</option>\n' +
                '                            {/volist}\n' +
                '                        </select></td>\n' +
                '                        <td><input type="text" name="mx[num][]" value="" class="layui-input mx_num" onblur="getMoneys(this)" \n' +
                '                                   lay-verify="required" oninput="digitalFiltering(this,2)"></td>\n' +
                '                         <td><input type="text" name="mx[kcNum][]" value="'+item.haveNum+'" class="layui-input kcNum disabled" readonly\n' +
                '                                   lay-verify="required" ></td>\n' +
                '                        <td><input type="text" name="mx[saleContractOrder][]" class="layui-input" value="'+item.saleContractOrder+'" readonly></td>\n' +
                '                        <td><input type="text" name="mx[hsContract][]" class="layui-input" value="" ></td>\n' +
                '                        <td>\n' +
                '                            <a href="javascript:" class="layui-btn layui-btn-xs layui-btn-danger"\n' +
                '                               onclick="delMx(\'\',this)">删除</a>\n' +
                '\n' +
                '                        </td>\n' +
                '                    </tr> ';
            index++;
        }
        $("#mxTbdy").append(str)
      layui.form.render();
    }

    function delMx(id = '', e) {
        if (id == '') {
            $(e).parent().parent().remove()
            return;
        }
        $.ajax({
            url: "{:url('removeMx')}",
            type: 'post',
            dataType: 'json',
            data: {id: id},
            success: function (res) {
                if (res.code != 0) {
                    layer.msg(res.msg)
                } else {
                    $(e).parent().parent().remove()
                }
            },
            error(e) {
                layer.msg('网络错误，请稍后')
            }
        })
    }

    function getMoneys(ojb) {

        let trObj = $(ojb).parent().parent();
        let num = trObj.find(".mx_num").val()
        let kcNum = trObj.find(".kcNum").val()
        if (kcNum==''){
            kcNum=0;
        }
        if (parseFloat(num)>parseFloat(kcNum)){
            layer.msg('数量不能大于库存数量')
            trObj.find(".mx_num").val('')
            return false;
        }
    }


    function getBjd() {
        let khId=$("#punterId").val();
        if (khId==''){
            layer.msg('请选择供应商');
            return
        }
        boxModel("{:url('addMx')}?khId="+khId,'关联采购合同','1000')
    }

function onSure() {
httpRequest("{:url('onSure')}",{id:"{$id}"},function (res) {
if (res.code==0){
    parent.parent.location.reload();
}
})
    }
    function onCancel() {
httpRequest("{:url('onCancel')}",{id:"{$id}"},function (res) {
if (res.code==0){
    parent.parent.location.reload();
}
})
    }
</script>
{/block}
<!-- /脚本 -->